package cn.s1995.sloth.bizs.event.listener;

import cn.s1995.sloth.bizs.event.ShopCartEvent;
import cn.s1995.sloth.bizs.event.order.ShopCartEventOrder;
import cn.s1995.sloth.bizs.oo.dto.ShopCartDTO;
import cn.s1995.sloth.bizs.oo.dto.ShopCartItemDTO;
import cn.s1995.sloth.bizs.oo.dto.ShopCartItemDiscountDTO;
import org.apache.commons.compress.utils.Lists;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * ShopCartListenner
 *
 * @author Lex
 * @description 购物车监听器
 * @date 2024/01/02 14:16
 */
@Component
public class ShopCartListener {
    /**
     * 将店铺下的所有商品归属到该店铺的购物车当中
     *
     * @param event#getShopCart()       购物车
     * @param event#shopCartItemDtoList 该购物车的商品
     * @return 是否继续组装
     */
    @EventListener(ShopCartEvent.class)
    @Order(ShopCartEventOrder.DEFAULT)
    public void defaultShopCartEvent(ShopCartEvent event) {
        ShopCartDTO shopCart = event.getShopCartDto();
        List<ShopCartItemDTO> shopCartItemDtoList = event.getShopCartItemDtoList();
        // 对数据进行组装
        List<ShopCartItemDiscountDTO> shopCartItemDiscountDtoList = Lists.newArrayList();
        ShopCartItemDiscountDTO shopCartItemDiscountDto = new ShopCartItemDiscountDTO();

        shopCartItemDiscountDto.setShopCartItems(shopCartItemDtoList);
        shopCartItemDiscountDtoList.add(shopCartItemDiscountDto);
        shopCart.setShopAvatar(shopCartItemDtoList.get(0).getShopAvatar());
        shopCart.setShopCartItemDiscounts(shopCartItemDiscountDtoList);
    }
}
